import { notification } from 'antd';
import { registerHandler } from './ws';
import * as MachineAction from './actions/machine';
import * as RunnableUnitAction from './actions/runnable-unit';

export function init(dispatch) {
  registerHandler('ALERT', ({ payload }) => {
    notification.info({
      message: '提示',
      description: payload
    });
  });

  registerHandler('MACHINE_STATUS_UPDATE', ({ payload }) => {
    if (payload.status === 'READY') {
      notification.success({
        message: '机器初始化成功',
        description: `机器 [${payload.ip}] 已经完成初始化`
      });
    } else {
      notification.warning({
        message: '机器初始化失败',
        description: `机器 [${payload.ip}] 初始化遇到问题`
      });
    }
    dispatch(MachineAction.updateMachineStatus(payload));
  });

  registerHandler('RUNNABLE_UNIT_DEPLOY_STATUS_UPDATE', ({ payload }) => {
    dispatch(RunnableUnitAction.updateDeployLogStatus(payload));
  });
}
